<?php 
include_once "cartlibs.php";
$msg = ""; 
    $_SESSION['muahangxong'] = ""; 
    if(@$_REQUEST['command']=='delete' ){ 
        remove_product($_REQUEST['idMay']); 
    } 
    if(@$_REQUEST['command']=='clear'){ 
        unset($_SESSION['GioHang']);       // huy het gio hang
    } 
    if(@$_REQUEST['command']=='update'){ 

        $max=count($_SESSION['GioHang']);    // lay tong so mat hang
        for($i=0;$i<$max;$i++){ 
            $idMay=$_SESSION['GioHang'][$i]['idMay']; 
            $SoLuong=intval($_REQUEST['SoLuong_'.$idMay]); 
            if($SoLuong>0 && $SoLuong<=999){ 
                $_SESSION['GioHang'][$i]['SoLuong']=$SoLuong;             
            } 
            else{ 
               echo "<script language='javascript'> alert ('Số lượng phải từ 1 đến 999');</script>" ;              
            } 
        } 
    }
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Gio hang</title>
<script language="javascript">
    function del(idMay){ 
        if(confirm('Bạn có chắc muốn xóa sản phẩm này ?')){   // hien thi ra 1 bang thong bao yes/no.
            document.capnhatgiohang.idMay.value=idMay; 
            document.capnhatgiohang.command.value='delete'; 
            document.capnhatgiohang.submit(); 
        } 
    } 
    function clear_cart(){  // chua dung. neu dung thi thay link cua trang xoagiohang.php o duoi
        if(confirm('Bạn có chắc muốn xóa hết sản phẩm trong giỏ hàng ?')){ 
            document.capnhatgiohang.command.value='clear'; 
            document.capnhatgiohang.submit(); 
        } 
    } 
    function update_cart(){ 
        document.capnhatgiohang.command.value='update'; 
        document.capnhatgiohang.submit(); 
    } 


</script>
<style type="text/css">
<!--
#style {
	
	border-top-width: 1px;
	border-right-width: 1px;
	border-bottom-width: 1px;
	border-left-width: 1px;
	border-top-style: none;
	border-right-style: solid;
	border-bottom-style: none;
	border-left-style: solid;

	background:#FFC;
}
-->
</style>

</head>


<body>
<?php include_once("cartlibs.php");
if(@is_array($_SESSION['GioHang']))
{

?>
<form action="" method="post" name="capnhatgiohang" id="capnhatgiohang">
        <input type="hidden" name="idMay" />
        <input type="hidden" name="command" />
<table width="100%" border="1" align="center" id="style">
  <tr align="center">
    <td>Xóa</td>
    <td>Hình</td>
    <td>Mã Máy</td>
    <td>Giá</td>
    <td>Số lượng</td>
    <td>Thành tiền</td>
  </tr>
  <?php
 $max=count($_SESSION["GioHang"]);
	for($i=0;$i<$max;$i++)  // lay het mat hang trong gio hang ra
	{	
		
		$idMay=$_SESSION["GioHang"][$i]["idMay"];
		$SoLuong=$_SESSION["GioHang"][$i]["SoLuong"];
		$Gia=$_SESSION["GioHang"][$i]["Gia"];
		$UrlHinh=$_SESSION['GioHang'][$i]["UrlHinh"];
		if($SoLuong==0)             // so luong san pham la 0 thi bo qua ^^
		continue;
		?>

  <tr align="center">
    <td><a href="#" onClick="javascript:del('<?php echo $idMay;?>')">Xóa</a></td>
    <td><img src="images/product/<?php echo $UrlHinh;?>" width="100" height="100"></td>
    <td><?php echo $idMay;?></td>
    <td><?php echo $Gia;?></td>
    <td><input name="SoLuong_<?php echo $idMay;?>" type="text" value="<?php echo $SoLuong; ?>" /></td>
    <td><?php echo $Gia*$SoLuong;?></td>
  </tr>
  <?php }?>
  <tr><td align="center" colspan="6">Tổng tiền : <?php echo get_order_total();?></td></tr>
  <tr><td colspan="6" align="center"><a href="#" onClick="javascript:update_cart()"><input type="button" value="Cập Nhật"></a><a href="index.php"><input type="button" value="Mua Tiếp"></a><a href="index.php?ACT=xoagiohang"><a href="#" onClick="javascript:clear_cart()"><input type="button" value="Xóa giỏ hàng"></a><a href="index.php?ACT=donhang"><input type="button" value="Mua hàng"></a></td></tr>
</table>
</form>
<?php }else { 
	echo('<center><font color="white"><h3>Your cart is empty<font color></center>');
}
?>
</body>
</html>